package com.mail.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author shengtao
 * @Description:
 * @date 2018/09/12 9:41
 */
@Slf4j
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public long sendText(String deliver, String[] receiver, String[] carbonCopy, String subject, String content) {
        long startTimestamp = System.currentTimeMillis();
        log.info("Start send mail ... ");
        try{

            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(deliver);
            message.setTo(receiver);
            message.setCc(carbonCopy);
            message.setSubject(subject);
            message.setText(content);
            mailSender.send(message);
            log.info("Send mail success, cost {} million seconds", System.currentTimeMillis() - startTimestamp);
        }catch (Exception e){
            log.error("Send mail failed, error message is : {} \n", e.getMessage());
            e.printStackTrace();
//            throw new ServiceException(4001,"");
        }
        return receiver.length;
    }

    @Override
    public void sendHtmlEmail(String deliver, String[] receiver, String[] carbonCopy, String subject, String content, boolean isHtml) {
        long startTimestamp = System.currentTimeMillis();
        log.info("Start send email ...");
        try{
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(deliver);
            helper.setTo(receiver);
            helper.setCc(carbonCopy);
            helper.setSubject(subject);
            helper.setText(content, isHtml);
            mailSender.send(message);
            log.info("Send email success, cost {} million seconds", System.currentTimeMillis() - startTimestamp);
        }catch (Exception e){
            log.error("Send email failed, error message is {} \n", e.getMessage());
            e.printStackTrace();
        }

    }

    @Override
    public void sendAttachmentFileEmail(String deliver, String[] receiver, String[] carbonCopy, String subject, String content, boolean isHtml, String fileName, File file) {
        long startTimestamp = System.currentTimeMillis();
        log.info("Start send mail ...");
        try {

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);

            messageHelper.setFrom(deliver);
            messageHelper.setTo(receiver);
            messageHelper.setCc(carbonCopy);
            messageHelper.setSubject(subject);
            messageHelper.setText(content, isHtml);
            messageHelper.addAttachment(fileName,file);
            messageHelper.addAttachment("副本"+fileName,file);
            mailSender.send(message);
            log.info("Send mail success, cost {} million seconds", System.currentTimeMillis() - startTimestamp);

        }catch (Exception e){
            log.error("Send email failed, error message is {} \n", e.getMessage());
            e.printStackTrace();
        }

    }

    @Override
    public void sendTemplateEmail(String deliver, String[] receiver, String[] carbonCopy, String subject, String template, Context context) {
        long startTimestamp = System.currentTimeMillis();
        log.info("Start send mail ...");
        try {

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);

            String content = templateEngine.process(template, context);
            messageHelper.setFrom(deliver);
            messageHelper.setTo(receiver);
            messageHelper.setCc(carbonCopy);
            messageHelper.setSubject(subject);
            messageHelper.setText(content, true);
            mailSender.send(message);
            log.info("Send mail success, cost {} million seconds", System.currentTimeMillis() - startTimestamp);

        }catch (Exception e){
            log.error("Send email failed, error message is {} \n", e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void sendOnlineImageEmail(String deliver, String[] receiver, String[] carbonCopy, String subject,String context, String srcPath, String srcName) {
        long startTimestamp = System.currentTimeMillis();
        log.info("Start send mail ...");
        try {

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            messageHelper.setFrom(deliver);
            messageHelper.setTo(receiver);
            messageHelper.setCc(carbonCopy);
            messageHelper.setSubject(subject);
            messageHelper.setText(context,true);
            FileSystemResource res = new FileSystemResource(new File(srcPath));
            messageHelper.addInline(srcName,res);


            mailSender.send(message);
            log.info("Send mail success, cost {} million seconds", System.currentTimeMillis() - startTimestamp);

        }catch (Exception e){
            log.error("Send email failed, error message is {} \n", e.getMessage());
            e.printStackTrace();
        }
    }
}
